package com.dcbeida.fragment.statistics;

import android.app.Activity;
import android.content.DialogInterface;
import android.support.annotation.Nullable;

import com.dcbeida.MyApplication;
import com.dcbeida.bean.AllShopSummary;
import com.dcbeida.bean.AllShopSummaryCurrent;
import com.dcbeida.bean.ShopSummary;
import com.dcbeida.bean.ShopSummaryCurrent;
import com.dcbeida.callback.DialogCallback;
import com.dcbeida.fragment.homeFragment.HomePresenter;
import com.dcbeida.model.ModelUtils;
import com.dcbeida.utils.JsonGet;
import com.dcbeida.utils.Utils;

import java.lang.reflect.Type;
import java.util.Calendar;

import okhttp3.Call;
import okhttp3.Response;

public class StatisticsPresent implements StatisticsContract.Present {


    private int drugId;

    private StatisticsContract.View view;

    private ModelUtils model;

    private Activity act;

    private int month;
    private int year;
    private int day;


    public int getDrugId() {
        return drugId;
    }

    public void setDrugId(int drugId) {
        this.drugId = drugId;
    }

    public StatisticsPresent(StatisticsContract.View view, Activity act) {
        //我这里直接把activity作为view，所以不需要
        //mView.setPresenter(this);
        this.view = view;
        model = ModelUtils.newInstance();
        this.act = act;

        Calendar cal = Calendar.getInstance();
        year = cal.get(Calendar.YEAR);
        month = cal.get(Calendar.MONTH) + 1;
        day = cal.get(Calendar.DATE);


    }

    public int getMonth() {
        return month;
    }

    //取消dialog 取消请求
    DialogInterface.OnCancelListener lis = new DialogInterface.OnCancelListener() {
        @Override
        public void onCancel(DialogInterface dialog) {
            model.getCancel();
        }
    };


    @Override
    public void getShopSummaryCurrent() {
        //当月统计接口
        if (drugId == 0) {
            model.getDate("getshopsummarycurrentv2", new JsonGet()
                    .putopt("uid", MyApplication.user.getUid())
                    .putopt("shopid", MyApplication.user.getShopid())
                    .tostring(), new ShopSummaryCurrentCallBack(act, AllShopSummaryCurrent.class, lis));
        } else {
            model.getDate("getshopsummarycurrentbydrugv2", new JsonGet()
                    .putopt("uid", MyApplication.user.getUid())
                    .putopt("shopid", MyApplication.user.getShopid())
                    .putopt("drugid", drugId)
                    .tostring(), new ShopSummaryCurrentCallBack(act, ShopSummaryCurrent.class, lis));
        }

    }


    final class ShopSummaryCurrentCallBack<T> extends DialogCallback<T> {

        public ShopSummaryCurrentCallBack(Activity activity, Type type, DialogInterface.OnCancelListener listener) {
            super(activity, type, listener,false);
        }

        @Override
        public void onResponse(T data) {
            if (data != null) {
                if(data instanceof ShopSummaryCurrent)
                    view.showShopSummaryCurrent((ShopSummaryCurrent) data);
                else
                    view.showShopSummaryCurrent((AllShopSummaryCurrent) data);

            }

        }


        public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
            super.onError(call, response, e);
            view.showTip("网络异常，请重试");
        }
    }


    @Override
    public void getShopSummary() {
        //当月统计接口
        String endate = "";
        if (12 != month) {
            endate = year + "-" + (month) + "-" + day + "";
        } else {
            endate = Utils.getNowTime();
        }

        if (drugId == 0) {
            model.getDate("getshopsummaryv2", new JsonGet()
                    .putopt("uid", MyApplication.user.getUid())
                    .putopt("begindate", year + "-01-01")
                    .putopt("shopid", MyApplication.user.getShopid())
                    .putopt("enddate", endate)
                    .putopt("reporttype", "1")
                    .tostring(), new ShopSummaryCallBack(act, AllShopSummary.class, lis));
        } else

            model.getDate("getshopsummarybydrugv2", new JsonGet()
                    .putopt("uid", MyApplication.user.getUid())
                    .putopt("shopid", MyApplication.user.getShopid())
                    .putopt("drugid", drugId)
                    .putopt("begindate", year + "-01-01")
                    .putopt("enddate", endate)
                    .putopt("reporttype", "1")
                    .tostring(), new ShopSummaryCallBack(act, ShopSummary.class, lis));
    }

    @Override
    public void start() {

    }

    final class ShopSummaryCallBack<T> extends DialogCallback<T> {

        public ShopSummaryCallBack(Activity activity, Type type, DialogInterface.OnCancelListener listener) {
            super(activity, type, listener);
        }

        @Override
        public void onResponse(T data) {
            if (data != null) {
                if(data instanceof ShopSummary)
                    view.showShopSummary((ShopSummary) data);
                else
                    view.showAllShopSummary((AllShopSummary) data);

            }

        }


        public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
            super.onError(call, response, e);
            view.showTip("网络异常，请重试");
        }
    }
}
